package com.sccba.emtbusiness.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.sccba.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sccba.emtbusiness.mapper.YjczUserChannelMapper;
import com.sccba.emtbusiness.domain.YjczUserChannel;
import com.sccba.emtbusiness.service.IYjczUserChannelService;

/**
 * 用户系统关联Service业务层处理
 * 
 * @author lichh
 * @date 2024-05-07
 */
@Service
public class YjczUserChannelServiceImpl implements IYjczUserChannelService 
{
    @Autowired
    private YjczUserChannelMapper yjczUserChannelMapper;

    /**
     * 查询用户系统关联
     * 
     * @param userId 用户系统关联主键
     * @return 用户系统关联
     */
    @Override
    public YjczUserChannel selectYjczUserChannelByUserId(Long userId)
    {
        return yjczUserChannelMapper.selectYjczUserChannelByUserId(userId);
    }

    /**
     * 查询用户系统关联列表
     * 
     * @param yjczUserChannel 用户系统关联
     * @return 用户系统关联
     */
    @Override
    public List<YjczUserChannel> selectYjczUserChannelList(YjczUserChannel yjczUserChannel)
    {
        return yjczUserChannelMapper.selectYjczUserChannelList(yjczUserChannel);
    }

    /**
     * 新增用户系统关联
     * 
     * @param yjczUserChannel 用户系统关联
     * @return 结果
     */
    @Override
    public int insertYjczUserChannel(YjczUserChannel yjczUserChannel)
    {
        return yjczUserChannelMapper.insertYjczUserChannel(yjczUserChannel);
    }

    /**
     * 修改用户系统关联
     * 
     * @param yjczUserChannel 用户系统关联
     * @return 结果
     */
    @Override
    public int updateYjczUserChannel(YjczUserChannel yjczUserChannel)
    {
        return yjczUserChannelMapper.updateYjczUserChannel(yjczUserChannel);
    }

    /**
     * 批量删除用户系统关联
     * 
     * @param userIds 需要删除的用户系统关联主键
     * @return 结果
     */
    @Override
    public int deleteYjczUserChannelByUserIds(Long[] userIds)
    {
        return yjczUserChannelMapper.deleteYjczUserChannelByUserIds(userIds);
    }

    /**
     * 删除用户系统关联信息
     * 
     * @param userId 用户系统关联主键
     * @return 结果
     */
    @Override
    public int deleteYjczUserChannelByUserId(Long userId)
    {
        return yjczUserChannelMapper.deleteYjczUserChannelByUserId(userId);
    }

    @Override
    public void insertOrUpdateUserChannel(Long channelId, Long channelUserId, String channelUserIds, boolean isInsert) {
        if (null == channelId || 0 == channelId) {
            return;
        }
        if (!isInsert) {
            yjczUserChannelMapper.deleteByChannelId(channelId);
        }
        if (StringUtils.isEmpty(channelUserIds) && null == channelUserId) {
            return;
        }
//        List<Long> userIdList = yjczUserChannelMapper.selectUserIdListByChannelId(channelId);
        List<YjczUserChannel> needAddUserChannel = new ArrayList<>();
        boolean isIncludeChannelUserId = false;
        YjczUserChannel userChannel;
        if (StringUtils.isNotEmpty(channelUserIds)) {
            String[] arr = channelUserIds.split(",");
            for (int i = 0; i < arr.length; ++ i) {
                Long userId = Long.parseLong(arr[i]);
                if (userId == channelUserId) {
                    isIncludeChannelUserId = true;
                }
//                int idx = userIdList.indexOf(userId);
//                if (idx >= 0) {
//                    userIdList.remove(idx);
//                    continue;
//                }
                userChannel = new YjczUserChannel();
                needAddUserChannel.add(userChannel);
                userChannel.setUserId(userId);
                userChannel.setChannelId(channelId);
            }
        }
        if (!isIncludeChannelUserId && null != channelUserId) {
//            int idx = userIdList.indexOf(channelUserId);
//            if (idx >= 0) {
//                userIdList.remove(idx);
//            } else {
            userChannel = new YjczUserChannel();
            userChannel.setChannelId(channelId);
            userChannel.setUserId(channelUserId);
            needAddUserChannel.add(userChannel);
//            }
        }
//        if (!userIdList.isEmpty()) {
//            yjczUserChannelMapper.batchDeleteByUserIdsAndChannelId(userIdList, channelId);
//        }
        if (!needAddUserChannel.isEmpty()) {
            yjczUserChannelMapper.batchInsert(needAddUserChannel);
        }
    }

    @Override
    public void deleteByChannelIds (Long[] channelIds) {
        yjczUserChannelMapper.deleteByChannelIds(channelIds);
    }

    @Override
    public void batchInsert(List<YjczUserChannel> userChannelList) {
        yjczUserChannelMapper.batchInsert(userChannelList);
    }

    @Override
    public void deleteByUserId(Long userId) {
        yjczUserChannelMapper.deleteByUserId(userId);
    }
}
